package codename.carrot.bomberman.models;

import sheep.graphics.Image;
import sheep.graphics.SpriteView;
import codename.carrot.bomberman.game.listener.ObstacleListener;

/**
 * Abstract class that is used by the game's obstacles that requires animations
 * @see {@link codename.carrot.bomberman.models.AbstractObstacle} - Abstract class for non animated obstacles
 * @author Group A4
 *
 */
public abstract class AbstractAnimatedObstacle extends AbstractObstacle {


	/**
	 * Array containing the images used to animate the object
	 */
	protected Image[] images;
	/**
	 * Current animation frame during runtime
	 */
	protected int currentFrame = 0;
	/**
	 * tickTime Time between each frame currentFrame First frame of animation
	 */
	protected float tickTime = 0.1f;
	/**
	 * Time left of the animation loop
	 */
	protected float timeLeft;

	protected float timeToLive;
	/**
	 * Defines whether the animation should loop or not
	 */
	protected boolean loop = true, 
			/**
			 * defines whether the animation has played at least once, or not
			 */
			hasPlayedOnce = false;

	/**
	 * Class constructor for object with a single animation loop
     * @param xCoor - X coordinates to spawn the obstacle at
     * @param yCoor - Y coordinates to spawn the obstacle at
     * @param images - Image to be used for the obstacle
     * @param listener - obstacle listener(s)
	 */
	public AbstractAnimatedObstacle(int xCoor, int yCoor, Image[] images,
			ObstacleListener listener) {
		super(xCoor, yCoor, images[0], listener);
		this.images = images;
	}
	/**
	 * Class constructor for object with multiple animation loops
     * @param xCoor - X coordinates to spawn the obstacle at
     * @param yCoor - Y coordinates to spawn the obstacle at
     * @param images - Image to be used for the obstacle
     * @param listener - obstacle listener(s)
     * @param loop - boolean value determining whether the animation should loop
	 */
	public AbstractAnimatedObstacle(int xCoor, int yCoor, Image[] images,
			ObstacleListener listener, boolean loop) {
		super(xCoor, yCoor, images[0], listener);
		this.images = images;
		this.loop = loop;
	}
	
	/**
	 * Class constructor for object with multiple animation loops and a set tick-time
	 * @param xCoor - X coordinates to spawn the obstacle at
	 * @param yCoor - Y coordinates to spawn the obstacle at
	 * @param images - Image to be used for the obstacle
	 * @param tickTime - time between each animation in the loop
	 * @see {@link codename.carrot.bomberman.models.AbstractAnimatedObstacle#tickTime} - the default tick time value
	 * @param currentFrame - current animation frame 
	 * @param listener - obstacle listener(s)
	 * @param loop - boolean for whether the animation should loop or not
	 
	 */
	public AbstractAnimatedObstacle(int xCoor, int yCoor, Image[] images,
			float tickTime, int currentFrame, ObstacleListener listener,
			boolean loop) {
		super(xCoor, yCoor, images[0], listener);
		this.images = images;
		this.tickTime = tickTime;
		this.currentFrame = currentFrame;
		this.loop = loop;
	}

	/**
	 * Overrides the default Sprite object update function. Handles object interaction and animation loop.
	 */
	@Override
	public void update(float dt) {
		super.update(dt);

		if (isVisible()) {
			timeLeft += dt;
			// Flip to next frame
			if (timeLeft >= tickTime && !hasPlayedOnce) {
				currentFrame = (currentFrame + 1) % images.length;
				setView((SpriteView) images[currentFrame]);
				timeLeft -= tickTime;

				if (!loop && currentFrame == images.length - 1)
					hasPlayedOnce = true;				
			}
		}
	}

}